package com.zhku.meeting.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.Room;
import com.zhku.meeting.entities.ShowStatus;
import com.zhku.meeting.mapper.MeetingRecordMapper;
import com.zhku.meeting.mapper.RoomMapper;
import com.zhku.meeting.mapper.ShowStatusMapper;
import com.zhku.meeting.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomService extends ServiceImpl<RoomMapper, Room> {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private ShowStatusMapper showStatusMapper;

    @Autowired
    private MeetingRecordMapper meetingRecordMapper;

    private Room room;
    private List<Room> rooms;

    public ResultVO<?> getRoom(Map<String, Object> para) {
        Integer id = Integer.parseInt((String) para.get("roomId"));
        Room room = roomMapper.selectByPrimaryKey(id);
        Map<String, Object> map = new HashMap<>();
        map.put("result", room);
        return ResultVO.successData(map);
    }

    public ResultVO<?> getAll(Map<String, Object> para) {
        Map<String, Object> map = new HashMap<>();
        if (para.containsKey("currentPage")) {
            Integer currentPage = Integer.parseInt((String) para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
            PageHelper.startPage(currentPage, pageSize);
            rooms = roomMapper.selectAll();
            PageInfo<Room> pageInfo = new PageInfo<>(rooms);
            map.put("result", pageInfo);
        } else {
            rooms = roomMapper.selectAll();
            Map<String, Object> result = new HashMap<>();
            result.put("list", rooms);
            map.put("result", result);
        }
        return ResultVO.successData(map);
    }

    public ResultVO<?> updateRoom(Map<String, Object> para) {
        Integer id = (Integer) para.get("roomId");
        String no = null;
        Integer size = null;
        Boolean status = null;
        if (!para.containsKey("roomStatus")) {
            no = (String) para.get("roomNo");
            size = (Integer) para.get("roomSize");
        } else {
            status = (Boolean) para.get("roomStatus");
        }
        room = Room.builder().roomNo(no).roomSize(size).roomStatus(status).build();
        room.setRoomId(id);
        int num = roomMapper.updateByPrimaryKey(room);
        return num > 0 ? ResultVO.success() : ResultVO.error();
    }

    public ResultVO<?> addRoom(Map<String, Object> para) {
        String no = (String) para.get("roomNo");
        Integer size = (Integer) para.get("roomSize");
        room = Room.builder().roomNo(no).roomSize(size).build();
        //连同状态表也插入
        showStatusMapper.insert(room);
        int num = roomMapper.insert(room);
        return num > 0 ? ResultVO.success() : ResultVO.error();
    }

    public ResultVO<?> deleteRoom(Map<String, Object> para) {
        Integer id = (Integer) para.get("roomId");
        int num = roomMapper.deleteByPrimaryKey(id);
        showStatusMapper.deleteByPrimaryKey(id);
        meetingRecordMapper.deleteByCondition(id);
        return num > 0 ? ResultVO.success() : ResultVO.error();
    }

    public ResultVO<?> selectByNo(Map<String, Object> para) {
        //取出信息
        String no = null;
        Integer size = null;
        Boolean status = null;
        if (para.containsKey("roomNo")) {
            no = "%" + para.get("roomNo") + "%";
        }
        if (para.containsKey("roomSize")) {
            size = Integer.parseInt((String) para.get("roomSize"));
        }
        if (para.containsKey("roomStatus")) {
            status = (Boolean) para.get("roomStatus");
        }
        Integer currentPage = Integer.parseInt((String) para.get("currentPage"));
        Integer pageSize = Integer.parseInt((String) para.get("pageSize"));

        //封装
        room = Room.builder().roomNo(no).roomSize(size).roomStatus(status).build();

        //分页
        PageHelper.startPage(currentPage, pageSize);
        List<Room> rooms = roomMapper.selectByNo(room);
        PageInfo pageInfo = new PageInfo(rooms);

        Map<String, Object> map = new HashMap<>();
        map.put("result", pageInfo);
        return ResultVO.successData(map);
    }

    //查重
    public ResultVO<?> duplicateCheck(Map<String, Object> para) {
        String roomNo = (String) para.get("roomNo");
        Integer roomId = null;
        if (para.containsKey("roomId")) {
            roomId = (Integer) para.get("roomId");
        }
        Room room = Room.builder().roomNo(roomNo).roomId(roomId).build();
        Room room1 = roomMapper.duplicateCheck(room);
        if (room1 != null) {
            return ResultVO.errorMessage("该会议室编号已经存在");
        }
        return ResultVO.success();
    }


    //根据占用的会议室id查询空闲的会议室
    public ResultVO<?> getFree(Map<String, Object> para) {
        Map<String, Object> map = new HashMap<>();
        List<ShowStatus> statuses;
        if (para.containsKey("currentPage")) {
            Integer currentPage = Integer.parseInt((String) para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
            PageHelper.startPage(currentPage, pageSize);
            //查出所有
            statuses = showStatusMapper.selectAll();
            PageInfo pageInfo = new PageInfo(statuses);
            map.put("result", pageInfo);
        } else {
            statuses = showStatusMapper.selectAll();
            map.put("result", statuses);
        }
        return ResultVO.successData(map);
    }

    public List<Room> getRooms() {
        return roomMapper.selectAll();
    }

    public ResultVO<?> updateEquipment(Map<String, Object> para) {
        Integer id = (Integer) para.get("roomId");
        Boolean air = null;
        Boolean projector = null;
        if (para.containsKey("air")) {
            air = (Boolean) para.get("air");
            showStatusMapper.updateEquipment(id, air, projector);
            roomMapper.updateEquipment(id, air, projector);
            return ResultVO.success();
        }
        projector = (Boolean) para.get("projector");
        showStatusMapper.updateEquipment(id, air, projector);
        roomMapper.updateEquipment(id, air, projector);
        return ResultVO.success();
    }
}
